<?php
/**
 *
 * @category   babel 
 * @package    BABEL
 * @author     Hannes Magnusson <hannes.magnusson@gmail.com>
 * @author     Audun Skjelvan
 * @copyright  2008 Team babel
 * @license    LGPL
 */


include "includes/babel/prepend_no_gui.php";
include 'classes/babel/Translation_Tasks.php';
include 'classes/babel/TranslationsPoints_Checks.php';
include "includes/babel/ajax_translation.php";

if (isset($_SESSION["task"]["last_task"])) {
	$task = @unserialize($_SESSION["task"]["last_task"]);
}
if (!($task instanceof Babel_Translation_Task)) {
	no("Error", "You have no tasks..");
}

if ($_POST["str"] == "Input translated text here") {
	no("Error", "Please insert suggestion, thank you!");
}

try {
	$task->setTranslatedString($_POST["str"]);
	$action = $task->compareStrings();
} catch(UnexpectedValueException $e) {
	no("Error", "Please insert suggestion, thank you!");
}

switch($action) {
case Babel_Translation_Task::NEW_STRING:	
	$task->updateTranslation($_SESSION["user"]["user_id"], BABEL_TRANSLATE_STATUS_VOTE, $_SESSION["task"]["last_id"]);
	$_SESSION["task"]["skip"] = 0;
	yes(Babel_Translation_Task::NEW_STRING);
	break;

case Babel_Translation_Task::CHANGED_STRING:

	$obj_tpl->setFile("babel_translate", "babel/translate_reason.html");
	$obj_tpl->setBlock("babel_translate", "task_reason_option", "option_ref");

	foreach($CHANGE_REASONS as $k => $v) {
		$obj_tpl->setVar(array(
			"VAR_REASON_VALUE" => $k,
			"VAR_REASON_TEXT"  => $v,
		));
		$obj_tpl->parse("option_ref", "task_reason_option", true);
	}
	$_SESSION["task"]["last_task"] = serialize($task);

	no("You changed the translation, why?", $obj_tpl->parse("babel_translate", "babel_translate"));

	break;

case Babel_Translation_Task::NO_CHANGE:
	// FIXME: The voting should be calculated from the users skill level...
	$task->vote($_SESSION["user"]["user_id"], BABEL_TRANSLATE_VOTE_REASON_OK);

	/* Reset the translation number */
	$_SESSION["task"]["skip"] = 0;
	yes(Babel_Translation_Task::NO_CHANGE);

	break;

default:
	break;
}
no("Error", "Unknown action");


